﻿@namespace BootstrapBlazor.Components
@inherits BootstrapModuleComponentBase
@attribute [BootstrapModuleAutoLoader("Filters/TableFilter.razor.js")]

@if (IsHeaderRow)
{
    @RenderFilter(Column)
}
else
{
    <span @attributes="@AdditionalAttributes" id="@Id" class="@ClassString" data-bb-dismiss=".filter-dismiss">
        <i class="@FilterClassString" data-bs-placement="bottom" data-bs-auto-close="outside" data-bs-toggle="bb.dropdown" data-bs-custom-class="shadow">
        </i>
        <div class="card filter-item">
            <div class="card-header"><span>@_title</span></div>
            <div class="card-body">
                @RenderFilter(Column)
            </div>
            <div class="card-footer">
                <div class="d-flex flex-fill">
                    <RenderTemplate>
                        @if (ShowMoreButton)
                        {
                            <Button Color="Color.None" OnClick="OnClickPlus" Icon="@PlusIcon" IsDisabled="@(_count == 1)"></Button>
                            <Button Color="Color.None" OnClick="OnClickMinus" Icon="@MinusIcon" IsDisabled="@(_count == 0)"></Button>
                        }
                    </RenderTemplate>
                </div>
                <Button Color="Color.None" class="filter-dismiss" OnClick="OnClickReset" Text="@ClearButtonText"></Button>
                <Button Color="Color.None" class="filter-dismiss" OnClick="OnClickConfirm" Text="@FilterButtonText"></Button>
            </div>
        </div>
    </span>
}

@code {
    RenderFragment<ITableColumn> RenderFilter => Column =>
    @<CascadingValue Value="this" IsFixed="true">
        @if (Column.FilterTemplate != null)
        {
            @Column.FilterTemplate
        }
        else if (Column.PropertyType.IsEnum())
        {
            <EnumFilter Type="Column.PropertyType" Count="_count"></EnumFilter>
        }
        else if (IsLookup)
        {
            <LookupFilter Lookup="_lookup.Value" LookupStringComparison="Column.LookupStringComparison" Type="Column.PropertyType" IsShowSearch="Column.ShowSearchWhenSelect"></LookupFilter>
        }
        else
        {
            var fieldType = Nullable.GetUnderlyingType(Column.PropertyType) ?? Column.PropertyType;
            switch (fieldType.Name)
            {
                case nameof(Boolean):
                    <BoolFilter />
                    break;
                case nameof(DateTime):
                    <DateTimeFilter Count="_count" />
                    break;
                case nameof(Int16):
                    <NumberFilter TType="Int16" Count="@_count" Step="@_step" />
                    break;
                case nameof(Int32):
                    <NumberFilter TType="Int32" Count="@_count" Step="@_step" />
                    break;
                case nameof(Int64):
                    <NumberFilter TType="Int64" Count="@_count" Step="@_step" />
                    break;
                case nameof(Single):
                    <NumberFilter TType="Single" Count="@_count" Step="@_step" />
                    break;
                case nameof(Double):
                    <NumberFilter TType="Double" Count="@_count" Step="@_step" />
                    break;
                case nameof(Decimal):
                    <NumberFilter TType="Decimal" Count="@_count" Step="@_step" />
                    break;
                case nameof(String):
                    <StringFilter Count="_count" />
                    break;
                default:
                    <div>@NotSupportedMessage</div>
                    break;
            };
        }
    </CascadingValue>;
}
